<template>
  <div class="app-container calendar-list-container">

    <el-container>
      <el-aside width="200px">
        <template>
          <div></div>
        </template>
      </el-aside>
      <el-container>
        <el-header>az</el-header>
        <el-main>
          <div></div>
        </el-main>
        <el-footer>
          <el-form ref="form" :model="formData">
            <el-form-item label="用户">
              <el-input type="text" v-model="formData.from"></el-input>
            </el-form-item>
            <el-form-item label="发送给">
              <el-input type="text" v-model="formData.to"></el-input>
            </el-form-item>
          <el-form-item>
              <el-input type="textarea" v-model="formData.desc"></el-input>
            </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="websocketsend">发送</el-button>
          </el-form-item>
          </el-form>
        </el-footer>

      </el-container>
    </el-container>

  </div>
</template>

<script>
export default {
  name: 'Msg',
  data() {
    return {
      formData: {
        desc: '',
        to: '',
        from: 'admin'
      },
      websock: null
    }
  },
  created() {
    this.initWebSocket()
  },
  methods: {
    threadPoxi() {
      const agentData = 'aaa'
      if (this.websock.readyState === this.websock.OPEN) {
        this.websocketsend(agentData)
      } else if (this.websock.readyState === this.websock.CONNECTING) {
        const that = this
        setTimeout(function() {
          that.websocketsend(agentData)
        }, 300)
      } else {
        this.initWebSocket()
        const that = this
        setTimeout(function() {
          that.websocketsend(agentData)
        }, 500)
      }
    },
    initWebSocket() {
      const wsuri = 'ws://localhost:8085/admin/websocket'
      this.websock = new WebSocket(wsuri)
      this.websock.onmessage = this.websocketonmessage
      this.websock.onclose = this.websocketclose
    },
    websocketonmessage(e) {
      const redata = JSON.parse(e.data)
      console.log(redata.value)
    },
    websocketsend(agentData) {
      this.websock.send(agentData)
    },
    websocketclose(e) {
      console.log('connection closed (' + e.code + ')')
    }
  }
}
</script>
